//
//  JKNavscrollChageViewController.m
//  JKNavscrollChage
//
//  Created by 王冲 on 2018/5/29.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "JKNavscrollChageViewController.h"

@interface JKNavscrollChageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
 
}
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIView *topNaView;
@end

@implementation JKNavscrollChageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.view addSubview:self.tableView];
    
    self.topNaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, JKNaviHeight)];
    self.topNaView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.topNaView];
    
    UIView *headView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, 200)];
    headView.backgroundColor = [UIColor redColor];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 10)];
    label.backgroundColor = [UIColor brownColor];
    [headView addSubview:label];
    
    self.tableView.tableHeaderView = headView;
    
    UIView *footView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, 20)];
    footView.backgroundColor = [UIColor redColor];
    self.tableView.tableFooterView = footView;
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:{
            return 3;
        }break;
        case 1:{
            return 5;
        }break;
        case 2:{
            return 10;
        }break;
        default:
            break;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    
    if (!cell) {
                
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.backgroundColor = JKRandomColor;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, 0.01)];
    sectionView.backgroundColor = backgrCOlor;
    return sectionView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UILabel *headviewLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 24, CIO_SCREEN_WIDTH, 24)];
    headviewLabel.text = [NSString stringWithFormat:@"这是第%ld段头",section];
    headviewLabel.font = [UIFont systemFontOfSize:20.f];
    headviewLabel.backgroundColor = [UIColor yellowColor];
    return headviewLabel;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 48;
    
}


-(UITableView* )tableView{
    
    if (!_tableView) {
        
        // UITableViewStyleGrouped
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, JKNaviHeight, CIO_SCREEN_WIDTH, CIO_SCREEN_HEIGHT-JKNaviHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
        
        
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor  = backgrCOlor;
    }
    return _tableView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if ([scrollView isEqual: self.tableView]) {
        
        
        CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
        
        if (translation.y>0) {
            
            DDLogDebug(@"下滑");
            [self scrolviewDown];
            
        }else if(translation.y<0){
            
            DDLogDebug(@"上滑");
            [self scrolviewUp];
        }
    }
}

#pragma mark 上滑
-(void)scrolviewUp{
    
    //DDLogDebug(@"上滑");
    
    [UIView animateWithDuration:0.24 animations:^{
        
        self.topNaView.y = -44;
        self.tableView.y = JKNaviHeight-44;
        
        self.tableView.height = CIO_SCREEN_HEIGHT-JKNaviHeight+44;
        
    }];
    
}


#pragma mark 下滑
-(void)scrolviewDown{
    
    //DDLogDebug(@"下滑");
    [UIView animateWithDuration:0.24 animations:^{
        
        self.topNaView.y = 0;
        self.tableView.y = JKNaviHeight;
        self.tableView.height = CIO_SCREEN_HEIGHT-JKNaviHeight;
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end


